package com.cs543.j.client.ui;

import java.io.FileNotFoundException;
import java.util.Vector;

import com.cs543.j.client.R;
import com.cs543.j.client.cloudService.Client;
import com.cs543.j.client.cloudService.LocalClient;
import com.cs543.j.client.cloudService.RemoteClient;
import com.cs543.j.client.cloudService.filesystem.Directory;
import com.cs543.j.client.cloudService.filesystem.Entry;
import com.cs543.j.client.cloudService.storage.CloudStorage;
import com.cs543.j.client.cloudService.storage.LocalStorage;
import com.cs543.j.client.cloudService.storage.Storage;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class StorageExplorerListAdapter implements ListAdapter{
	
	private Vector<Entry> entryList;
	private Directory currentDirectory;
	
	private Directory toParent;

	public StorageExplorerListAdapter(Entry[] list,  Directory currentDirector) {
		super();	
		
		this.entryList = new Vector<Entry>();
		this.currentDirectory = currentDirector;
		this.toParent = new Directory("..", currentDirector.getParent());
				
		if(list != null)
		{
			for(Entry each : list)
			{
				this.entryList.add(each);
			}
		}
		
		if(!this.currentDirectory.isRootDirectory())
			this.entryList.add(0, toParent);
	}
	
	public boolean isActionToParent(Entry entry)
	{
		return this.toParent == entry;
	}

	public int getCount() {
		return this.entryList.size();
	}

	public Object getItem(int position) {
		return this.entryList.get(position);
	}

	public long getItemId(int position) {
		return 1;
	}

	public int getItemViewType(int position) {
		return 1;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater layoutInflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = layoutInflater.inflate(R.layout.client_list_viewer_element, null);
		
		ImageView icon = (ImageView) view.findViewById(R.id.client_list_viewer_element_icon);
		TextView text = (TextView) view.findViewById(R.id.client_list_viewer_element_text);
		
		Entry entry = entryList.get(position);
		
		if(entry.isDirectory())
		{
			icon.setImageResource(R.drawable.directory);
		}
		else
		{
			icon.setImageResource(R.drawable.file);
		}
		
		text.setText( entry.getName() );
			
		return view;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isEmpty() {
		return this.entryList.isEmpty();
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		
	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int position) {
		return true;
	}
	
	

}
